<?php

use \system\core\selector as selector;

class filter extends html\core\plugin
{

	function initialize()
	{
		$this->document = $this->getCaller();
	}

	function filterTag($expr)
	{
		print_r($this->document->getTags());
		echo "Filter Tags by Expr: ".$expr."\n";
	}

	function hasClass($name="")
	{
		if( $name == "*" ) $name = "";
		foreach( $this->document->getTags() as $arrayId => $tag )
		{
			if( $tag->attributeManager()->has('class~="'.$name.'"') )
			{
				$selection[$arrayId] = $tag;
			}
		}
		$this->document->setTags($selection);
	}

	function hasTag($tagname)
	{
		foreach( $this->document->getTags() as $arrayId => $tag )
		{
			if( strtolower($tag->getTagname()) == strtolower($tagname) && ($tag->isOpenTag() || $tag->isSingleTag()) )
			{
				$selection[$arrayId] = $tag;
			}
		}
		$this->document->setTags($selection);
	}

	function hasID($name="")
	{
		if( $name == "*" ) $name = "";
		foreach( $this->document->getTags() as $arrayId => $tag )
		{
			if( $tag->attributeManager()->has('id="'.$name.'"') )
			{
				$selection[$arrayId] = $tag;
			}
		}
		$this->document->setTags($selection);
	}

	function hasAttribute($string="")
	{
		if( $name == "*" ) $name = "";
		foreach( $this->document->getTags() as $arrayId => $tag )
		{
			if( $tag->attributeManager()->has($string) )
			{
				$selection[$arrayId] = $tag;
			}
		}
		$this->document->setTags($selection);
	}

	function toString()
	{
		echo "Yess!";
		exit;
	}

	function select($string)
	{
		$string = trim($string);
		$selector = selector::parser($string);
//		echo "Full selector: \"".$string."\"\n";
//		print_r($selector);
//		print_r($this->document->getTagById(1));
//		exit;

		$this->document->executeSelection($this->document, $selector);

//		exit;
	}

	function executeSelection($document, $selector)
	{
		if( !$document->getTags() ) return false;

//		echo "Part \"".$selector->getSelector()."\" of Type: {$selector->getTargetType()}\n";
//		print_r($document);
//		print_r($selector);
		switch ($selector->getTargetType())
		{
			case "CLASS":
				$match = $document->hasClass($selector->getSelector());
				break;
			case "ID":
				$match = $document->hasID($selector->getSelector());
				break;
			case "ATTRIBUTE":
				$match = $document->hasAttribute($selector->getSelector());
				break;
			case "TAG":
				$match = $document->hasTag($selector->getSelector());
				break;
			case "PSEUDO":
				$sel = "[".substr($selector->getSelector(), 1)."=]";
				$match = $document->hasAttribute($sel);
				break;
			case "MULTI":
				$match = $document;
				foreach( $selector->getNextRule() as $id => $rulePart )
				{
					$multiDoc[$id] = clone $document;
					$document->executeSelection($multiDoc[$id], $rulePart);
					if( is_array($matchMulti) && $multiDoc[$id]->getTags() )
					{
						$matchMulti = array_merge($matchMulti, $multiDoc[$id]->getTags());
					}
					elseif( is_array($matchMulti) )
					{

					}
					else
					{
						$matchMulti = $multiDoc[$id]->getTags();
					}
				}

				$this->document->setTags($matchMulti);
				return;
				break;
		}

//		$match->plotTags();

		if( $selector->hasNextRule() )
		{
//			echo "Prepare next rule!\n";
			$nextRule = $selector->getNextRule();
			$lookupCode = $nextRule[0]->getLookupFilter();
//			echo selector::readLookupFilter($lookupCode)."\n";

			switch (selector::translateLookupFilter($lookupCode))
			{
				case "LOOK_AT_THIS.ELEMENT":
					$nextSet = $match;
					break;
				case "LOOK_AT_ALL.CHILDREN":
					$nextSet = clone $match;
//						$c->setTags($match->getAllChildren());
//						print_r($c->getEveryChildren());
					$nextSet->setTags($match->getEveryChildren());
					break;
				case "LOOK_AT_NEXT.CHILDREN":
					$nextSet = clone $match;
					$nextSet->setTags($match->getAllChildren());
					break;
				case "LOOK_AT_NEXT.SIBLINGS":
					$nextSet = clone $match;
					$nextSet->setTags($match->getNextSibling());
					break;
				default:
					die("Missing document-set!");
					break;
			}

			$match->executeSelection($nextSet, $nextRule[0]);
		}
	}

	function plotTags()
	{
		if( $this->document->getTags() ) foreach( $this->document->getTags() as $id => $tag )
			{
				$str.= sprintf("%04d ", $id).str_repeat("· ", $tag->getLevel() - 1).$tag->getTagname()." - ".$tag->getLevel()."\n";
			}
		echo $str;
	}

	function plotTag($id)
	{
		$tags = $this->document->getTagById($id);
		echo "Plot: ".$id."\n";
		print_r($tags);
	}

	function each($callback)
	{
		foreach($this->document->getTags() as $id => $tag){
			$callback($tag,$id);
		}
	}

}

?>
